<?php
// $Id: template.php,v 1.16.2.1 2009/02/25 11:47:37 goba Exp $

/**
 * Adds body-tag class 'sidebar-left', 'sidebar-right' or 'sidebars' classes as needed.
 */
function phptemplate_body_class($left, $right) {
  if ($left != '' && $right != '') {
    $class = 'sidebars';
  }
  else {
    if ($left != '') {
      $class = 'sidebar-left';
    }
    if ($right != '') {
      $class = 'sidebar-right';
    }
  }

  if (isset($class)) {
    print ' class="'. $class .'"';
  }
}

/**
 * Return a themed breadcrumb trail.
 */
function phptemplate_breadcrumb($breadcrumb) {
  if (!empty($breadcrumb)) {
    return '<div class="breadcrumb">'. implode(' › ', $breadcrumb) .'</div>';
  }
}

/**
 * Override or insert PHPTemplate variables into the templates.
 */
function phptemplate_preprocess_page(&$vars) {
  $vars['tabs2'] = menu_secondary_local_tasks();
}

/**
 * Theme the menu
 */
function phptemplate_menu_local_tasks() {
  return menu_primary_local_tasks();
}

function phptemplate_comment_submitted($comment) {
  return t('!datetime — !username',
    array(
      '!username' => theme('username', $comment),
      '!datetime' => format_date($comment->timestamp)
    ));
}

function phptemplate_node_submitted($node) {
  return t('!datetime — !username',
    array(
      '!username' => theme('username', $node),
      '!datetime' => format_date($node->created),
    ));
}

/**
* Add easy classes/id's to menu items
*/
function phptemplate_menu_item($link, $has_children, $menu = '', $in_active_trail = FALSE, $extra_class = NULL) {
  $class = ($menu ? 'expanded' : ($has_children ? 'collapsed' : 'leaf'));
  if (!empty($extra_class)) {
    $class .= ' '. $extra_class;
  }
  if ($in_active_trail) {
    $class .= ' active-trail';
  }
 
  // Add unique identifier
  static $item_id = 0;
  $item_id += 1;
  $id .= ' ' . 'menu-item-custom-id-' . $item_id;
  // Add semi-unique class
  $class .= ' ' . preg_replace("/[^a-zA-Z0-9]/", "", strip_tags($link));
 
  return '<li class="'. $class .'" id="' . $id . '">'. $link . $menu ."</li>\n";
}

/**
* Adds a meta line that tells users they're logged in
*/
function phptemplate_user_id_bar() {
	
	global $user;
		if ($user->uid) {
			print '<div id="UserIDContainer" class="LoggedIn"><div id="UserID">';
			print "You are logged in as <a href='". base_path() ."user/" . $user->uid . "'> "  . $user->name . "</a> | ";
			print l("Log Out ","logout");
		}
		else {
			print '<div id="UserIDContainer" class="NotLoggedIn"><div id="UserID">';
			print l("Login ","user");
		}
	print '</div><!-- /UserID --></div><!-- /UserIDContainer -->';
}

/**
* Adds a few links for adding nodes
*/

function phptemplate_nodenav() {
	$linklist = array(
  		'Dashboard' => array( 'title' => 'Dashboard', 'href' => 'admin' ),
  		'Content' => array( 'title' => 'Add Page', 'href' => 'admin/content/node/overview' ),
  		'Comments' => array( 'title' => 'Add Story', 'href' => 'admin/content/comment' )
	);
		
	$attributelist = array('class' => 'NodeNav');
	global $user; 
	if ($user->uid) {
		print theme('links', $linklist, $attributelist);
	}
}


/**
* Adds a list of the last 10 nodes that have been updated or created
*/

drupal_add_js('misc/collapse.js');

function phptemplate_nodelist() {
	
	$list_no = 10;
	
	$query = "SELECT DISTINCT n.nid, n.title, n.created, n.changed
	  FROM {node} n
	  WHERE n.type = 'page' OR n.type = 'story' OR n.type = 'image' OR n.type = 'gallery' OR n.type = 'blog' AND n.status = 1
	  ORDER BY n.changed DESC
	  LIMIT $list_no";
	
	$sql = db_rewrite_sql($query);
	$result = db_query($sql);
	$items = array();
	
	while ($anode = db_fetch_object($result)) {
	
	  	$trunctitle = truncate_utf8($anode->title, 30, TRUE, TRUE);
	
	  $items[]= 
    	'<div class="title">' .
        l($trunctitle, "node/$anode->nid") .
        '</div>' .
        '<div class="edit">' .
	    l('Edit', "node/$anode->nid/edit", array('title' => 'Edit this item')) .
        '</div>' ;
	}
	
	if(count($items)) {
		global $user;
		if ($user->uid) {
			print '<fieldset class="collapsible collapsed">';
			print "<legend>Recent edits</legend>";
	  		print theme('item_list',$items);
	  		print "</fieldset>";
	  	}
	}
}

// Breakout vocabularies into separate arrays
function phptemplate_preprocess_node(&$vars) {
  // If we have any terms...
  if ($vars['node']->taxonomy) {
    // Let's iterate through each term.
    foreach ($vars['node']->taxonomy as $term) {
      // We will build a new array where there will be as many
      // nested arrays as there are vocabularies
      // The key for each nested array is the vocabulary ID.     
      $vocabulary[$term->vid]['taxonomy_term_'. $term->tid]  = array(
         'title' => $term->name,
         'href' => taxonomy_term_path($term),
         'attributes' => array(
         'rel' => 'tag', 
         'title' => strip_tags($term->description),
        ),
      );       
    }
    $vars['vocab1']= theme('links',$vocabulary[1], array('class'=>'links inline'));
    $vars['vocab2']= theme('links',$vocabulary[2], array('class'=>'links inline'));
    $vars['vocab3']= theme('links',$vocabulary[3], array('class'=>'links inline'));
  }
}
